package com.sskj.givecoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.adapter.BaseAdapter;
import com.sskj.givecoin.common.adapter.ViewHolder;
import com.sskj.givecoin.common.widget.SelectView;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.p.activity.VerifyJuniorPresenter;
import com.sskj.givecoin.utils.CheckUtil;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import java.util.Arrays;

import butterknife.BindView;

/**
 * ProjectName：期权
 * DESC: (类描述)初级实名认证
 * Created by hqf on 2018/10/19 10:23
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class VerifyJuniorActivity extends BaseActivity<VerifyJuniorPresenter> {


    @BindView(R.id.toolBar)
    ToolBarLayout toolBar;
    @BindView(R.id.real_name_verify)
    EditText realNameVerify;
    @BindView(R.id.junnor_verify_spinner)
    SelectView junnorVerifySpinner;
    @BindView(R.id.id_verify)
    EditText idVerify;
    @BindView(R.id.tel_verify)
    EditText telVerify;  // 手机号地址
    @BindView(R.id.address_verify)
    EditText addressVerify;  // 邮箱地址
    @BindView(R.id.next_btn)
    Button nextBtn;
    @BindView(R.id.high_verity)
    TextView highVerity;
    private int type = 1;  // 默认身份证

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_verify_junior;
    }

    @Override
    public VerifyJuniorPresenter getPresenter() {
        return new VerifyJuniorPresenter();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, VerifyJuniorActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        String[] data = getResources().getStringArray(R.array.verifyJunnor);
        junnorVerifySpinner.setText(data[0]);
        BaseAdapter<String> adapter = new BaseAdapter<String>(R.layout.item_spinner_exchange, Arrays.asList(data), null) {
            @Override
            public void bind(ViewHolder holder, String item) {
                TextView textView = holder.getView(R.id.text);
                textView.setText(item);
                textView.setOnClickListener(v -> {
                    type=holder.getAdapterPosition()+1;
                    junnorVerifySpinner.setText(item);
                    junnorVerifySpinner.dismiss();
                });
            }
        };
        junnorVerifySpinner.bindAdapter(adapter);
    }

    @Override
    protected void onViewBinding() {
        super.onViewBinding();
        toolBar.setTitle(App.INSTANCE.getString(R.string.strVerifyJuniorActivity200));
        toolBar.setLeftButtonOnClickLinster(v -> finish());
        if (CheckUtil.isEmail(MyAppLication.getMobile())) {
            addressVerify.setText(MyAppLication.getMobile());
            addressVerify.setEnabled(false);
        } else {
            telVerify.setText(MyAppLication.getMobile());
            telVerify.setEnabled(false);
        }
        ClickUtil.click(nextBtn, () -> {
            String realName = realNameVerify.getText().toString().trim();
            String idNum = idVerify.getText().toString().trim();
            String telNum = telVerify.getText().toString().trim();
            String address = addressVerify.getText().toString().trim();
            if (TextUtils.isEmpty(realName)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyJuniorActivity203));
                return;
            }

            if (TextUtils.isEmpty(idNum)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyJuniorActivity204));
                return;
            }
            if (type == 1) {  // 身份证校验
                if (idNum.length() != 18) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyJuniorActivity205));
                    return;
                }
            }
            if (!TextUtils.isEmpty(telNum)) {
                if (!CheckUtil.isMobile(telNum)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyJuniorActivity207));
                    return;
                }
            }
            if (TextUtils.isEmpty(address)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyJuniorActivity208));
                return;
            }
            if (!CheckUtil.isEmail(address)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyJuniorActivity209));
                return;
            }
            mPresenter.verifyJunior(telNum, address, idNum, realName, type);
        });
        ClickUtil.click(highVerity, () -> {
            VerifyGuideActivity.start(this);
        });
    }


    public void updateSuccess() {
        finish();
    }
}
